package com.ssy.lingxi.workflow.controller;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.workflow.api.model.vo.request.*;
import com.ssy.lingxi.workflow.api.model.vo.response.ComplexTaskCompleteVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleTaskCompleteVO;
import com.ssy.lingxi.workflow.service.IProcessExecutionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 流程任务执行接口
 * @author 万宁
 * @version 2.0.0
 * @date 2021-04-29
 */
@RestController
@RequestMapping("/workflow/process")
public class ProcessExecutionController {

    @Resource
    private IProcessExecutionService processExecutionService;

    /**
     * （各种流程通用）启动流程
     * @param startVO 接口参数
     * @return 启动结果
     */
    @RequestMapping(value = "/start", method = RequestMethod.POST)
    public Wrapper<ComplexTaskCompleteVO> startProcess(@RequestBody @Valid TaskStartVO startVO) {
        return processExecutionService.startProcess(startVO);
    }

    /**
     * （各种流程通用）启动流程后，执行第一个Task
     * @param startVO 接口参数
     * @return 执行结果
     */
    @RequestMapping(value = "/start/exec/first", method = RequestMethod.POST)
    public Wrapper<ComplexTaskCompleteVO> startProcessThenCompleteFirstTask(@RequestBody @Valid TaskStartVO startVO) {
        return processExecutionService.startProcessThenCompleteFirstTask(startVO);
    }

    /**
     * （各种流程通用）执行流程步骤
     * @param executeVO 接口参数
     * @return 执行结果
     */
    @RequestMapping(value = "/exec", method = RequestMethod.POST)
    public Wrapper<ComplexTaskCompleteVO> completeTask(@RequestBody @Valid TaskExecuteVO executeVO) {
        return processExecutionService.completeTask(executeVO);
    }

    /**
     * （各种流程通用）连续执行流程步骤
     * @param executeVO 接口参数
     * @return 执行结果
     */
    @RequestMapping(value = "/exec/serial", method = RequestMethod.POST)
    public Wrapper<ComplexTaskCompleteVO> completeSerialTasks(@RequestBody @Valid SerialTaskExecuteVO executeVO) {
        return processExecutionService.completeSerialTasks(executeVO);
    }

    /**
     * 启动单一的外部流程或内部流程
     * @param startVO 接口参数
     * @return 启动结果
     */
    @RequestMapping(value = "/simple/start", method = RequestMethod.POST)
    public Wrapper<SimpleTaskCompleteVO> startSimpleProcess(@RequestBody @Valid TaskStartVO startVO) {
        return processExecutionService.startSimpleProcess(startVO);
    }

    /**
     * 启动单一的外部流程或内部流程，并执行第一个步骤
     * @param startVO 接口参数
     * @return 执行结果
     */
    @RequestMapping(value = "/simple/start/exec/first", method = RequestMethod.POST)
    public Wrapper<SimpleTaskCompleteVO> startSimpleProcessThenCompleteFirstTask(@RequestBody @Valid TaskStartVO startVO) {
        return processExecutionService.startSimpleProcessThenCompleteFirstTask(startVO);
    }

    /**
     * 执行单一的外部流程、或内部流程
     * @param executeVO 接口参数
     * @return 执行结果
     */
    @RequestMapping(value = "/simple/exec", method = RequestMethod.POST)
    public Wrapper<SimpleTaskCompleteVO> completeSimpleTask(@RequestBody @Valid TaskExecuteVO executeVO) {
        return processExecutionService.completeSimpleTask(executeVO);
    }

    /**
     * （各种流程通用）查询流程参数(dataId)
     * @param queryVO 接口参数
     * @return 参数列表
     */
    @RequestMapping(value = "/data", method = RequestMethod.POST)
    public Wrapper<List<Long>> findProcessDataIds(@RequestBody @Valid ProcessDataQueryVO queryVO) {
        return processExecutionService.findProcessDataIds(queryVO);
    }

    /**
     * （各种流程通用）根据任务Id，（异步）删除流程实例
     * @param taskIdVO 接口参数
     */
    @RequestMapping(value = "/instance/delete", method = RequestMethod.POST)
    public void deleteProcessInstance(@RequestBody @Valid TaskIdVO taskIdVO) {
        processExecutionService.deleteProcessInstance(taskIdVO);
    }
}
